<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>品牌管理</title>
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <link rel="stylesheet" href="../plugins/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="../plugins/adminLTE/css/AdminLTE.css">
    <link rel="stylesheet" href="../plugins/adminLTE/css/skins/_all-skins.min.css">
    <link rel="stylesheet" href="../css/style.css">
    <script src="../plugins/jQuery/jquery-2.2.3.min.js"></script>
    <script src="../plugins/bootstrap/js/bootstrap.min.js"></script>

    <!--引入host文件中配置的域名   这是一个自定义的文件-->
    <script src="../js/pages/public.js"></script>

</head>
<body class="hold-transition skin-red sidebar-mini">
<!-- .box-body -->
<div class="box-header with-border">
    <h3 class="box-title">品牌管理</h3>
</div>

<div class="box-body">

    <!-- 数据表格 -->
    <div class="table-box">

        <!--工具栏-->
        <div class="pull-left">
            <div class="form-group form-inline">
                <div class="btn-group">
                    <button type="button" class="btn btn-default" title="新建" data-toggle="modal"
                            data-target="#insertEditModal"><i class="fa fa-file-o"></i> 新建
                    </button>
                    <button type="button" class="btn btn-default" title="删除" onclick="deleteBrand()"><i
                            class="fa fa-trash-o"></i> 删除
                    </button>
                    <button type="button" class="btn btn-default" title="刷新" onclick="window.location.reload();"><i
                            class="fa fa-refresh"></i> 刷新
                    </button>
                    <hr>
                    <form id="formId">
                        <input name="name" class="form-control" placeholder="品牌名称">
                        <input name="firstChar" class="form-control" placeholder="首字母">
                        <button type="button" class="btn btn-default" title="搜索" onclick="findBrandList()"><i
                                class="fa fa-refresh"></i> 搜索
                        </button>
                    </form>
                </div>
            </div>
        </div>
        <div class="box-tools pull-right">
            <div class="has-feedback">

            </div>
        </div>
        <!--工具栏/-->

        <!--数据列表-->
        <table id="dataList" class="table table-bordered table-striped table-hover dataTable">
            <thead>
            <tr>
                <th class="" style="padding-right:0px">
                    <input id="selall" type="checkbox" class="icheckbox_square-blue">
                </th>
                <th class="sorting_asc">品牌ID</th>
                <th class="sorting">品牌名称</th>
                <th class="sorting">品牌首字母</th>
                <th class="text-center">操作</th>
            </tr>
            </thead>
            <tbody id="tbodyId">
            <!--			                          <tr>-->
            <!--			                              <td><input  type="checkbox" ></td>			                              -->
            <!--				                          <td>1</td>-->
            <!--									      <td>联想</td>									     -->
            <!--		                                  <td>L</td>		                                 -->
            <!--		                                  <td class="text-center">                                           -->
            <!--		                                 	  <button type="button" class="btn bg-olive btn-xs" data-toggle="modal" data-target="#editModal"  >修改</button>                                           -->
            <!--		                                  </td>-->
            <!--			                          </tr>-->
            </tbody>
        </table>
    </div>
    <!--数据列表/-->
    <form id="indexForm">
        <a href="javascript:toPage(3)">首页</a>
        <a href="javascript:toPage(1)">上一页</a>
        <a href="javascript:toPage(2)">下一页</a>
        <a href="javascript:toPage(4)">尾页</a>
        第<input type="text" name="pageNum" id="pageNum" size="1px" readonly="readonly">页
        共<input type="text" name="pages" id="pages" size="1px" disabled="disabled">页
        共<input type="text" name="total" id="total" size="1px" disabled="disabled">条
    </form>
</div>
<!-- /.box-body -->

<!-- 增加窗口 -->
<div class="modal fade" id="insertEditModal" tabindex="-1" role="dialog" aria-labelledby="insertmyModalLabel"
     aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                <h3 id="insertmyModalLabel">品牌编辑</h3>
            </div>
            <div class="modal-body">
                <form id="BrandId">
                    <table class="table table-bordered table-striped" width="800px">
                        <tr>
                            <td>品牌名称</td>
                            <td><input name="name" class="form-control" placeholder="品牌名称"></td>
                        </tr>
                        <tr>
                            <td>首字母</td>
                            <td><input name="firstChar" class="form-control" placeholder="首字母 注意：只能写单个字母"></td>
                        </tr>
                    </table>
                </form>
            </div>
            <div class="modal-footer">
                <button class="btn btn-success" data-dismiss="modal" aria-hidden="true" onclick="insertBrand()">保存
                </button>
                <button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>
            </div>
        </div>
    </div>
</div>
<!--修改窗口-->
<div class="modal fade" id="updateEditModal" tabindex="-1" role="dialog" aria-labelledby="updatemyModalLabel"
     aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                <h3 id="updatemyModalLabel">品牌编辑</h3>
            </div>
            <div class="modal-body">
                <form id="BrandId1">
                    <input type="hidden" id="id1" name="id">
                    <table class="table table-bordered table-striped" width="800px">
                        <tr>
                            <td>品牌名称</td>
                            <td><input id="name1" name="name" class="form-control" placeholder="品牌名称"></td>
                        </tr>
                        <tr>
                            <td>首字母</td>
                            <td><input id="firstChar1" name="firstChar" class="form-control" placeholder="首字母"></td>
                        </tr>
                    </table>
                </form>
            </div>
            <div class="modal-footer">
                <button class="btn btn-success" data-dismiss="modal" aria-hidden="true" onclick="updateBrandList()">保存
                </button>
                <button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>
            </div>
        </div>
    </div>
</div>

<script>
    $(function () {
        getBrandList();
    })

    function getBrandList() {
        $.ajax({
            url: domain + "/brand/brandList",
            headers:{'Autherizetion':window.localStorage.getItem("token")},//获取浏览器缓存中的token进行过滤器放过
            // contentType: "application/json;charset=utf-8",//我将来要往前台发送数据的时候是以json的格式发送的
            dataType: "json",
            type: "post",//必须指定请求类型
            data: $("#indexForm").serialize(),//往后台发送的确确实实是一个json数据  JSON.stringify({})
            success: function (result) {
                if (result.code == 11) {//操作成功
                    // console.log(result)
                    var data = result.data.rows;
                    var html = "";
                    for (let i = 0; i < data.length; i++) {
                        // console.log(result.data[i].name)
                        html += " <tr>\n" +
                            "\t\t\t                              <td><input  type=\"checkbox\" name='check' value='" + data[i].id + "'></td>\t\t\t                              \n" +
                            "\t\t\t\t                          <td>1</td>\n" +
                            "\t\t\t\t\t\t\t\t\t      <td>" + data[i].name + "</td>\t\t\t\t\t\t\t\t\t     \n" +
                            "\t\t                                  <td>" + data[i].firstChar + "</td>\t\t                                 \n" +
                            "\t\t                                  <td class=\"text-center\">                                           \n" +
                            "\t\t                                 \t  <button type=\"button\" class=\"btn bg-olive btn-xs\" data-toggle=\"modal\" data-target=\"#updateEditModal\"  onclick='updateBrand(" + data[i].id + ")'>修改</button>                                           \n" +
                            "\t\t                                  </td>\n" +
                            "\t\t\t                          </tr>";
                        $("#tbodyId").html(html);
                    }
                    $("#pageNum").val(result.data.pageNum)
                    $("#pages").val(result.data.pages)
                    $("#total").val(result.data.total)
                }else if (result.code == 12){
                    top.window.location.href="../login.html";//token无效进行登录
                }
            }, error: function () {
                alert("品牌管理异常，请联系管理员")
            }
        })
    }

    function findBrandList() {
        var formId = $("#formId").serializeObject();//serializeObject 是自定义文件中自定义的一个方法
        $.ajax({
            url: domain + "/brand/findBrandList",
            headers:{'Autherizetion':window.localStorage.getItem("token")},//获取浏览器缓存中的token进行过滤器放过
            contentType: "application/json;charset=utf-8",//我将来要往前台发送数据的时候是以json的格式发送的
            dataType: "json",
            type: "post",//必须指定请求类型
            data: JSON.stringify(formId),//往后台发送的确确实实是一个json数据
            success: function (result) {
                if (result.code == 11) {//操作成功
                    // console.log(result)
                    var html = "";
                    for (let i = 0; i < result.data.length; i++) {
                        // console.log(result.data[i].id)
                        html += " <tr>\n" +
                            "\t\t\t                              <td><input  type=\"checkbox\" name='check' value='" + result.data[i].id + "'></td>\t\t\t                              \n" +
                            "\t\t\t\t                          <td>1</td>\n" +
                            "\t\t\t\t\t\t\t\t\t      <td>" + result.data[i].name + "</td>\t\t\t\t\t\t\t\t\t     \n" +
                            "\t\t                                  <td>" + result.data[i].firstChar + "</td>\t\t                                 \n" +
                            "\t\t                                  <td class=\"text-center\">                                           \n" +
                            "\t\t                                 \t  <button type=\"button\" class=\"btn bg-olive btn-xs\" data-toggle=\"modal\" data-target=\"#updateEditModal\" onclick='updateBrand(" + result.data[i].id + ")' >修改</button>                                           \n" +
                            "\t\t                                  </td>\n" +
                            "\t\t\t                          </tr>";
                        $("#tbodyId").html(html);
                    }
                }else if (result.code == 12) {
                    top.window.location.href="../login.html";//token失效 跳转回登录界面  重新登录生成token  放进缓存  重新获取
                }
            }, error: function () {
                alert("品牌管理异常，请联系管理员")
            }
        })
    }

    //增加
    function insertBrand() {
        var brandId = $("#BrandId").serializeObject();
        console.log(brandId)
        $.ajax({
            url: domain + "/brand/insertBrand",
            headers:{'Autherizetion':window.localStorage.getItem("token")},//获取浏览器缓存中的token进行过滤器放过
            contentType: "application/json;charset=utf-8",//我将来要往前台发送数据的时候是以json的格式发送的
            dataType: "json",
            type: "post",//必须指定请求类型
            data: JSON.stringify(brandId),//往后台发送的确确实实是一个json数据
            success: function (result) {
                if (result.code == 11) {
                    alert("增加成功")
                } else if (result.code == 12) {
                    top.window.location.href="../login.html";//token失效 跳转回登录界面  重新登录生成token  放进缓存  重新获取
                }
            }
        })
    }

    //删除
    function deleteBrand() {
        var arr = [];
        var check = document.getElementsByName("check");
        for (let i = 0; i < check.length; i++) {
            if (check[i].checked) {
                arr.push(check[i].value)
            }
        }
        if (arr >= 1) {
            console.log(arr)
            if (confirm("确认要删除吗？")) {
                $.ajax({
                    url: domain + "/brand/deleteBrand/" + arr,
                    headers:{'Autherizetion':window.localStorage.getItem("token")},//获取浏览器缓存中的token进行过滤器放过
                    dcontentType: "application/json;charset=utf-8",//我将来要往前台发送数据的时候是以json的格式发送的
                    dataType: "json",
                    type: "delete",//必须指定请求类型
                    // data:{arr:arr},//往后台发送的确确实实是一个json数据
                    success: function (result) {
                        if (result.code == 11) {
                            alert("删除成功")
                        } else if (result.code == 12) {
                            top.window.location.href="../login.html";//token失效 跳转回登录界面  重新登录生成token  放进缓存  重新获取
                        }
                    }
                })
            }

        }else{
            alert("请选择要删除的数据")
        }
    }

    //回显
    function updateBrand(id) {
        $.ajax({
            url: domain + "/brand/getBrandById",
            headers:{'Autherizetion':window.localStorage.getItem("token")},//获取浏览器缓存中的token进行过滤器放过
            dcontentType: "application/json;charset=utf-8",//我将来要往前台发送数据的时候是以json的格式发送的
            dataType: "json",
            type: "get",//必须指定请求类型
            data: {id: id},//往后台发送的确确实实是一个json数据
            success: function (result) {
                if (result.code == 11){
                    console.log("下面是回显:")
                    console.log(result)
                    $("#name1").val(result.data.name)
                    $("#firstChar1").val(result.data.firstChar)
                    $("#id1").val(result.data.id)
                }else if (result.code == 12){
                    top.window.location.href="../login.html";//token失效 跳转回登录界面  重新登录生成token  放进缓存  重新获取
                }

            }
        })
    }

    //修改
    function updateBrandList() {
        var BrandId1 = $("#BrandId1").serializeObject();
        console.log(BrandId1)
        $.ajax({
            url: domain + "/brand/updateBrandList",
            headers:{'Autherizetion':window.localStorage.getItem("token")},//获取浏览器缓存中的token进行过滤器放过
            contentType: "application/json;charset=utf-8",//我将来要往前台发送数据的时候是以json的格式发送的
            dataType: "json",
            type: "put",//必须指定请求类型
            data: JSON.stringify(BrandId1),//往后台发送的确确实实是一个json数据
            success: function (result) {
                if (result.code == 11) {
                    alert("修改成功")
                } else if (result.code == 12) {
                    top.window.location.href="../login.html";//token失效 跳转回登录界面  重新登录生成token  放进缓存  重新获取
                }
            }
        })
    }

    /*分页*/
    function toPage(id) {
        var pageNum = parseInt($("#pageNum").val());
        var pages = parseInt($("#pages").val());
        if (id == 1) {
            if (pageNum <= 1) {
                alert("已是首页");
                return;
            }
            $("#pageNum").val(pageNum - 1);
        } else if (id == 2) {
            if (pageNum >= pages) {
                alert("已是尾页");
                return;
            }
            $("#pageNum").val(pageNum + 1);
        } else if (id == 3) {
            $("#pageNum").val(1);
        } else if (id == 4) {
            $("#pageNum").val(pages);
        }
        getBrandList();
    }

</script>
</body>
</html>